<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <el-form :inline="true" :model="where" label-width="89px" @keyup.enter.native="reload"
        @submit.native.prevent>
        <el-form-item label="游戏包名称: ">
          <PageSelect :placeholder="'请选择游戏包'" v-model="where.package_id" :whereName="'package_name'"
            :url="'/Package/getChannelPackage'" :optionLable="'package_name'" :optionvalue="'id'" :multiple="false" />
        </el-form-item>
        <el-form-item label="推广员: ">
          <PageSelect :placeholder="'请选择推广员'" v-model="where.promote_id" :whereName="'realname'"
            :url="'/userpromote/index'" :optionLable="'realname'" :optionvalue="'id'" :multiple="false" />
        </el-form-item>
        <el-form-item label="推广员ID: ">
          <el-input
                v-model="where.promote_id"
                placeholder="请输入推广员ID"
              ></el-input>
        </el-form-item>
        <el-form-item label="审核状态: ">
          <el-select style="width: 100%" v-model="where.status">
            <el-option label="待审核" :value="0"></el-option>
            <el-option label="已审核" :value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="打包状态: ">
          <el-select style="width: 100%" v-model="where.enable_status">
            <el-option label="未打包" :value="0"></el-option>
            <el-option label="打包完成" :value="1"></el-option>
            <el-option label="准备打包" :value="2"></el-option>
            <el-option label="打包失败" :value="-1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <div class="ele-form-actions">
            <el-button type="primary" icon="el-icon-search" class="ele-btn-icon" @click="reload">查询
            </el-button>
            <el-button @click="reset">重置</el-button>
          </div>
        </el-form-item>

      </el-form>
      <!-- 数据表格 -->
      <ele-pro-table ref="table" :where="where" :datasource="url" :columns="columns" :selection.sync="selection"
        height="calc(100vh - 315px)">
        <!-- 表头工具栏 -->
        <template slot="toolbar">
          <!-- <el-button
            size="small"
            type="primary"
            icon="el-icon-plus"
            class="ele-btn-icon"
            @click="openEdit(null)"
            v-if="permission.includes('sys:apply:add')"
            >添加
          </el-button> -->
          <!-- <el-button
            size="small"
            type="danger"
            icon="el-icon-delete"
            class="ele-btn-icon"
            @click="removeBatch"
            v-if="permission.includes('sys:apply:dall')"
            >删除
          </el-button> -->
        </template>
        <!-- 操作列 -->
        <!-- <template slot="action" slot-scope="{ row }"> -->
        <template slot="action" slot-scope="{ row }">
          <el-dropdown>
            <el-button type="primary">
              操作列表<i class="el-icon-arrow-down el-icon--right"></i>
            </el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item v-if="row.status === 0" @click.native="status(row)">
                <el-link type="primary" :underline="false">审核 </el-link>
              </el-dropdown-item>
              <el-dropdown-item v-if="row.status === 1 && (row.enable_status === 1 || row.enable_status === -1)"
                @click.native="setEnableStatus(row)">
                <el-link type="primary" :underline="false">重新打包 </el-link>
              </el-dropdown-item>
              <el-dropdown-item v-if="row.enable_status === 1" @click.native="copy(row)">
                <el-link type="primary" :underline="false">复制落地页地址 </el-link>
              </el-dropdown-item>
              <el-dropdown-item v-if="row.enable_status === 1" @click.native="copyDown(row)">
                <el-link type="primary" :underline="false">复制固定下载地址 </el-link>
              </el-dropdown-item>
              <el-dropdown-item v-if="row.enable_status === 1" @click.native="copyUniversal(row)">
                <el-link type="primary" :underline="false">复制通用下载地址 </el-link>
              </el-dropdown-item>
              <el-dropdown-item v-if="row.enable_status === 1 && row.sdk_version == 3" @click.native="copyH5(row)">
                <el-link type="primary" :underline="false">复制H5下载地址 </el-link>
              </el-dropdown-item>
              <el-dropdown-item v-if="row.enable_status === 1 && row.sdk_version == 4" @click.native="copyPC(row)">
                <el-link type="primary" :underline="false">复制PC下载地址 </el-link>
              </el-dropdown-item>
              <el-dropdown-item v-if="row.pc_down_resources" @click.native="copyPCDown(row)">
                <el-link type="primary" :underline="false" >复制pc游戏下载资源 </el-link>
              </el-dropdown-item>
              <el-dropdown-item v-if="row.enable_status === 1 && row.sdk_version == 4" @click.native="copyPCLanding(row)">
                <el-link type="primary" :underline="false" >复制pc落地页地址 </el-link>
              </el-dropdown-item>
              <el-dropdown-item v-if="permission.includes('sys:package:setPackagelandingPagelandingPage')"
                @click.native="openLandingPage(row, true)">
                <el-link type="primary" :underline="false">落地页模板配置 </el-link>
              </el-dropdown-item>
              <el-dropdown-item v-if="permission.includes('sys:apply:delete')" @click.native="remove(row)">
                <el-link type="primary" :underline="false" >删除 </el-link>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
          <!-- <el-popconfirm
            class="ele-action"
            title="确定要删除此推广员分包吗？"
            @confirm="remove(row)"
          >
            <el-link
              type="danger"
              slot="reference"
              :underline="false"
              icon="el-icon-delete"
              v-if="permission.includes('sys:apply:delete')"
              >删除
            </el-link>
          </el-popconfirm> -->
        </template>
      </ele-pro-table>
    </el-card>
    <!-- 编辑弹窗 -->
    <ApplyEdit :data="current" :visible.sync="showEdit" @done="reload" />
    <package-landingPage :data="current" :visible.sync="showlandingPage" @done="reload" />
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import ApplyEdit from "./apply-edit";
import PageSelect from "@/components/pageSelect/index.vue";
import PackageLandingPage from "./package-landingPage";

export default {
  name: "ExamApply",
  components: { ApplyEdit, PageSelect, PackageLandingPage },
  computed: {
    ...mapGetters(["permission"])
  },
  data() {
    return {
      // 表格数据接口
      url: "/apply/index",
      // 表格列配置
      columns: [
        {
          columnKey: "selection",
          type: "selection",
          width: 45,
          align: "center",
          fixed: "left"
        },
        {
          prop: "id",
          label: "ID",
          width: 60,
          align: "center",
          showOverflowTooltip: true,
          fixed: "left"
        },
        {
          prop: "package_name",
          label: "游戏包",
          showOverflowTooltip: true,
          minWidth: 200,
          align: "center"
        },
        {
          prop: "promote_name",
          label: "推广员",
          showOverflowTooltip: true,
          minWidth: 200,
          align: "center"
        },
        {
          prop: "apply_time",
          label: "申请时间",
          showOverflowTooltip: true,
          minWidth: 200,
          align: "center",
          formatter: (row, column, cellValue) => {
            return this.$util.toDateString(cellValue);
          }
        },
        {
          prop: "status",
          label: "审核状态",
          showOverflowTooltip: true,
          minWidth: 200,
          align: "center",
          formatter: (row, column, cellValue) => {
            if (cellValue == 0) {
              return "待审核";
            } else if (cellValue == 1) {
              return "已审核";
            }
          }
        },
        {
          prop: "enable_status",
          label: "打包状态",
          showOverflowTooltip: true,
          minWidth: 200,
          align: "center",
          formatter: (row, column, cellValue) => {
            if (cellValue == 0) {
              return "未打包";
            } else if (cellValue == 1) {
              return "打包完成";
            } else if (cellValue == 2) {
              return "准备打包";
            } else if (cellValue == -1) {
              return "打包失败";
            }
          }
        },
        {
          prop: "sdk_version",
          label: "区别版本",
          showOverflowTooltip: true,
          minWidth: 200,
          align: "center",
          formatter: (row, column, cellValue) => {
            if (cellValue == 1) {
              return "安卓";
            } else if (cellValue == 2) {
              return "苹果";
            } else if (cellValue == 3) {
              return "H5";
            } else if (cellValue == 4) {
              return "PC";
            } else if (cellValue == 5) {
              return "小程序";
            }
          }
        },
        // {
        //   prop: 'dispose_name',
        //   label: '操作人',
        //   showOverflowTooltip: true,
        //   minWidth: 200,
        //   align: 'center'
        // },
        // {
        //   prop: 'dispose_time',
        //   label: '操作时间',
        //   showOverflowTooltip: true,
        //   minWidth: 200,
        //   align: 'center',
        //   formatter: (row, column, cellValue) => {
        //     return this.$util.toDateString(cellValue)
        //   }
        // },
        {
          prop: "update_time",
          label: "更新时间",
          sortable: "custom",
          showOverflowTooltip: true,
          minWidth: 160,
          align: "center",
          formatter: (row, column, cellValue) => {
            return this.$util.toDateString(cellValue);
          }
        },
        {
          columnKey: "action",
          label: "操作",
          width: 200,
          align: "center",
          resizable: false,
          slot: "action",
          fixed: "right"
        }
      ],
      // 表格搜索条件
      where: {},
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示landingPage编辑弹窗
      showlandingPage: false,
      // 是否显示编辑弹窗
      showEdit: false
    };
  },
  methods: {
    //删除分包
    remove(row){
      this.$http
        .post("/Apply/delete", { id: row.id })
        .then(res=>{
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
            this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        })
    },
    setEnableStatus(val) {
      this.$http
        .post("/Apply/setEnableStatus", { id: val.id, enable_status: 2 })
        .then(res => {
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
            this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        });
    },
    status(val) {
      this.$http.post("/Apply/status", { id: val.id, status: 1 }).then(res => {
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
          this.reload();
        } else {
          this.$message.error(res.data.msg);
        }
      });
    },
    /* 刷新表格 */
    reload() {
      this.$refs.table.reload({ where: this.where });
    },
    /* 重置搜索 */
    reset() {
      this.where = {};
      this.reload();
    },
    /* 显示编辑 */
    openEdit(row) {
      this.current = row;
      this.showEdit = true;
    },
    openLandingPage(row) {
      this.current = row;
      this.showlandingPage = true;
    },
    copy(row) {
      // 获取row中的下载地址
      const downloadUrl = row.pack_url;

      this.$copyText(downloadUrl);

      // 提示复制成功
      this.$message.success("下载地址已成功复制");
    },
    copyUniversal(row) {
      // 获取row中的下载地址
      const downloadUrl = row.down_url_new;

      this.$copyText(downloadUrl);

      // 提示复制成功
      this.$message.success("通用下载地址已成功复制");
    },
    copyH5(row) {
      // 获取row中的下载地址
      const downloadUrl = row.h5_url;

      this.$copyText(downloadUrl);

      // 提示复制成功
      this.$message.success("h5下载地址已成功复制");
    },
    copyPC(row) {
      // 获取row中的下载地址
      const downloadUrl = row.pc_url;

      this.$copyText(downloadUrl);

      // 提示复制成功
      this.$message.success("PC下载地址已成功复制");
    },
    copyPCLanding(row){
      // 获取row中的下载地址
      const downloadUrl = row.pc_url;

      this.$copyText(downloadUrl);

      // 提示复制成功
      this.$message.success("pc落地页地址已成功复制");
    },
    copyDown(row) {
      // 获取row中的下载地址
      const downloadUrl = row.web_url;

      this.$copyText(downloadUrl);

      // 提示复制成功
      this.$message.success("固定下载地址已成功复制");
    },
    /* 删除 */
    //   remove(row) {
    //     const loading = this.$loading({ lock: true })
    //     this.$http
    //       .post('/apply/delete', { id: row.id })
    //       .then((res) => {
    //         loading.close()
    //         if (res.data.code === 0) {
    //           this.$message.success(res.data.msg)
    //           this.reload()
    //         } else {
    //           this.$message.error(res.data.msg)
    //         }
    //       })
    //       .catch((e) => {
    //         loading.close()
    //         this.$message.error(e.message)
    //       })
    //   },
    //   /* 批量删除 */
    //   removeBatch() {
    //     if (!this.selection.length) {
    //       this.$message.error('请至少选择一条数据')
    //       return
    //     }
    //     this.$confirm('确定要删除选中的推广员分包吗?', '提示', {
    //       type: 'warning'
    //     })
    //       .then(() => {
    //         const loading = this.$loading({ lock: true })
    //         this.$http
    //           .post('/apply/delete', { id: this.selection.map((d) => d.id) })
    //           .then((res) => {
    //             loading.close()
    //             if (res.data.code === 0) {
    //               this.$message.success(res.data.msg)
    //               this.reload()
    //             } else {
    //               this.$message.error(res.data.msg)
    //             }
    //           })
    //           .catch((e) => {
    //             loading.close()
    //             this.$message.error(e.message)
    //           })
    //       })
    //       .catch(() => {})
    //   }
  }
};
</script>

<style scoped></style>
